package org.exez.modules.visitor.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.exez.common.api.vo.Result;
import org.exez.common.aspect.annotation.AutoLog;
import org.exez.common.system.base.controller.JeecgController;
import org.exez.common.system.vo.LoginUser;
import org.exez.modules.system.entity.SysLog;
import org.exez.modules.system.service.ISysUserService;
import org.exez.modules.visitor.constant.AuthorityEnum;
import org.exez.modules.visitor.entity.Device;
import org.exez.modules.visitor.entity.ReservationApplication;
import org.exez.modules.visitor.service.IDeviceService;
import org.exez.modules.visitor.service.SjyService;
import org.exez.modules.visitor.util.AES;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

 /**
 * @Description: 终端设备
 * @Author: exez-boot
 * @Date:   2020-06-20
 * @Version: V1.0
 */
@Slf4j
@Api(tags="终端设备")
@RestController
@RequestMapping("/visitor/device")
public class DeviceController extends JeecgController<Device, IDeviceService> {
	@Autowired
	private IDeviceService deviceService;


	 @Autowired
	 private ISysUserService userService;

	 @Autowired
	 private SjyService sjyService;


	 /**
	 * 分页列表查询
	 *
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@AutoLog(value = "终端设备-分页列表查询")
	@ApiOperation(value="终端设备-分页列表查询", notes="终端设备-分页列表查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "deviceCode", value = "设备编码", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "institutionId", value = "单位id", required = false, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageNo", value = "页码", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "页码大小", required = true, dataType = "Integer", paramType = "query"),
	})
	@GetMapping(value = "/list")
	public Result<IPage<Device>> queryPageList(@RequestParam(name="deviceCode", required = false) String deviceCode,
											   @RequestParam(name="institutionId", required = false) Integer institutionId,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		Result<IPage<Device>> result = new Result<>();

		LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<>();
		if (StringUtils.isNotBlank(deviceCode)) {
			queryWrapper.like(Device::getDeviceCode,deviceCode);
		}

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		if (null != institutionId) {
			queryWrapper.eq(Device::getInstitutionId, institutionId);
		}else if (!AuthorityEnum.ONE_USER.getIndex().equals(sysUser.getAuthority())) {
			queryWrapper.in(Device::getInstitutionId, AES.integerList(sysUser.getInstitutionId()));
		}

		queryWrapper.eq(Device :: getDeleted,1);

		Page<Device> page = new Page<Device>(pageNo, pageSize);
		IPage<Device> pageList = deviceService.page(page, queryWrapper);
		result.successSetDate(pageList);
		return result;
	}


	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "终端设备-通过id删除")
	@ApiOperation(value="终端设备-通过id删除", notes="终端设备-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {

		return Result.ok("删除成功!");
	}


	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "终端设备-通过id查询")
	@ApiOperation(value="终端设备-通过id查询", notes="终端设备-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Device device = deviceService.getById(id);
		return Result.ok(device);
	}


	 /**
	  * 终端设备刷新
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "终端设备刷新")
	 @ApiOperation(value="终端设备刷新", notes="终端设备刷新")
	 @GetMapping(value = "/refresh")
	 public Result<Boolean> refresh(@RequestParam(name="id",required=true) Integer id) {
		 Result<Boolean> result = new Result<>();

		 Boolean heartbeat = sjyService.getHeartbeat(id);

		 result.successSetDate(heartbeat);
		 return result;
	 }

}
